package com.haiertvbic.tvnow.widget;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import org.apache.http.client.ClientProtocolException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.haiertvbic.tvnow.R;
import com.haiertvbic.tvnow.model.ScreenShot;
import com.haiertvbic.tvnow.network.EPGWeb;
import com.haiertvbic.tvnow.network.NetUtil;

public class PipCache {

	private static HashMap<String, ScreenShot> pipMap = new HashMap<String, ScreenShot>();
	private static int listLength = 3;
	/**
	 * pip和gallery同用这一个缓存
	 */
	public static HashMap<String, ArrayList<ScreenShot>> pipListMap;

	public static void setPipMap(ScreenShot screen, String cid) {
		if (screen.getBm() != null) {
			if (pipMap.containsKey(cid) && pipMap.get(cid) != null) {
				pipMap.remove(cid);
				pipMap.put(cid, screen);
			} else {
				pipMap.put(cid, screen);
			}
		}

	}

	/**
	 * key为cid
	 * 
	 * @return
	 */
	public static HashMap<String, ScreenShot> getPipMap() {
		return pipMap;
	}

	public static ArrayList<ScreenShot> getRealTimeShort(Context context,
			String cid) {

		ArrayList<ScreenShot> pipList = new ArrayList<ScreenShot>();
		if (pipListMap == null) {
			pipListMap = new HashMap<String, ArrayList<ScreenShot>>();
		}
		if (pipListMap.containsKey(cid) && pipListMap.get(cid) != null
				&& pipListMap.get(cid).size() > 0) {
			pipList = pipListMap.get(cid);
			ScreenShot screen = null;
			if (NetUtil.getNetworkInfo(context)) {
				ArrayList<ScreenShot> screenList = null;
				try {
					screenList = EPGWeb.getChannelPIPXML(context, cid, 1);
					screen = screenList != null && screenList.size() > 0 ? screen = screenList
							.get(0) : null;
					if (screen != null && screen.getBm() != null) {
						pipList.add(0, screen);
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				// 无网络得不到图片,从本地取
				screen = getBmFromLocal(context, cid);
				pipList.add(0, screen);
			}

			if (pipList.size() > listLength) {
				pipList.remove(listLength);
			}
		} else {
			pipList = copyThreePictrue(context, cid, pipList);
		}
		if (pipListMap != null) {
			pipListMap.clear();
			pipListMap.put(cid, pipList);
		}

		return pipList;
	}

	/**
	 * 拷贝三同的图片图片
	 * 
	 * @param context
	 * @param cid
	 * @param pipList
	 * @return
	 */
	private static ArrayList<ScreenShot> copyThreePictrue(Context context,
			String cid, ArrayList<ScreenShot> pipList) {
		if (pipMap.containsKey(cid) && pipMap.get(cid) != null) {
			for (int i = 0; i < listLength; i++) {
				pipList.add(pipMap.get(cid));
			}
		} else {
			pipList = getThreeBmFromLocal(context);
		}
		return pipList;
	}

	/**
	 * 从本地读图片
	 * 
	 * @param context
	 * @return
	 */
	private static ArrayList<ScreenShot> getThreeBmFromLocal(Context context) {
		ArrayList<ScreenShot> bms_temp = null;
		Bitmap bm = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.tv_now);
		if (bm != null) {
			int ii = 3;
			bms_temp = new ArrayList<ScreenShot>();
			for (int i = 0; i < ii; i++) {
				// 第一次同时填充三张同样的图，加快获取图片的时间
				ScreenShot screen = new ScreenShot();
				screen.setBm(bm);
				bms_temp.add(screen);
			}
		}
		return bms_temp;
	}

	/**
	 * 从本地随机取图
	 * 
	 * @param context
	 * @return
	 */
	public static ScreenShot getBmFromLocal(Context context, String cid) {
		int[] RIds = { R.drawable.tv_now3, R.drawable.tv_now1,
				R.drawable.tv_now2 };
		int bmID = RIds[new Random().nextInt(RIds.length)];
		Bitmap bm = BitmapFactory.decodeResource(context.getResources(), bmID);
		ScreenShot screenNoNet = new ScreenShot();
		screenNoNet.setBm(bm);
		screenNoNet.setImageId(bmID + "");
		screenNoNet.setPipUrl(bmID + "");
		return screenNoNet;
	}
}
